<%--
  Created by IntelliJ IDEA.
  User: tum0r
  Date: 2019-03-13
  Time: 14:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="util.Decode" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="server.*" %>
<%@ page import="config.ApplicationConfig" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
    request.setCharacterEncoding(pageConfig.get("encode"));
    response.setCharacterEncoding(pageConfig.get("encode"));
    Object user = session.getAttribute("user");
%>
<html>
<head>
    <title>信 息</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/page.css">
    <link rel="stylesheet" href="css/player.css">
    <script src="js/echarts.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse head" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#navbar">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <%
                SettingServer settingServer = new SettingServer();
                settingServer.open();
                JSONObject logo = settingServer.readSettingObject("logo");
                if (logo.getString("logo").equals("")) {
            %>
            <img class="navbar-brand" src="image/logo.png">
            <%
            } else {
                FileServer fileServer = new FileServer();
                fileServer.open();
            %>
            <img class="navbar-brand" src="<%=fileServer.getImage(logo.getString("logo"))%>">
            <%
                    fileServer.close();
                }
            %>
            <a class="navbar-brand" href="#"><%=logo.getString("name")%>
            </a>
        </div>
        <div class="collapse navbar-collapse" id="navbar">
            <ul class="nav navbar-nav">
                <li>
                    <a href="index.jsp">首 页</a>
                </li>
                <li>
                    <a href="game.jsp">比 赛</a>
                </li>
                <li>
                    <a href="rank.jsp">排 名</a>
                </li>
                <li>
                    <a href="about.jsp">通 知</a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right text-center">
                <%
                    LoginServer loginServer = new LoginServer();
                    loginServer.open();
                    Object cookie = session.getAttribute("cookie");
                    if (user == null || user.toString().equals("") || cookie == null || cookie.toString().equals("") || loginServer.checkLogin(user.toString(), cookie.toString())) {
                        session.removeAttribute("user");
                        session.removeAttribute("cookie");
                        session.setAttribute("infoTitle", "未登录");
                        session.setAttribute("infoContent", "您还未登录，请先登录");
                        session.setAttribute("return", "index.jsp");
                        session.setAttribute("panelColor", "red");
                        loginServer.close();
                        response.sendRedirect("information.jsp");
                        return;
                    } else {
                        int user_type = loginServer.getUserType(user.toString());
                        if (user_type == 1) {
                            loginServer.close();
                            response.sendRedirect("mine");
                            return;
                        }
                %>
                <li>
                    <a href="mine" class="glyphicon glyphicon-user active">&nbsp;<%=user%>
                    </a>
                </li>
                <li>
                    <a href="#" class="glyphicon glyphicon-log-out" data-toggle="modal"
                       data-target="#logout">&nbsp;退出</a>
                    <div class="modal fade" id="logout" role="dialog" aria-hidden="true"
                         aria-labelledby="logoutLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('logout_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="logoutLabel">退 出 登 录</h4>
                                </div>
                                <div class="modal-body">
                                    确定要退出吗？
                                    <form id="logout_form" action="logout" method="post">
                                        <input type="text" name="username" hidden="hidden" value="<%=user%>">
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('logout_form'))">&nbsp;退出
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <%
                    }
                %>
            </ul>
        </div>
    </div>
</nav>

<div class="container main text-center">
    <div class="panel-body text-center">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h2 class="panel-title">个人信息</h2>
            </div>
            <div id="mine">
                <div class="panel-body">
                    <%
                        UserServer userServer = new UserServer();
                        userServer.open();
                        JSONObject result = userServer.searchUserByName(user.toString());
                        if (result.size() != 0 && JSONObject.fromObject(result.getString("icon")).size() != 0) {
                            JSONObject icon = JSONObject.fromObject(result.getString("icon"));
                            FileServer fileServer = new FileServer();
                            fileServer.open();
                            String uuid = icon.getString("uuid");
                            String name = icon.getString("name");
                    %>
                    <img src="<%=fileServer.getImage(uuid)%>" class="img-circle icon_mouse" alt="<%=name%>"
                         width=170 height=170 data-toggle="modal" data-target="#changeIcon"
                         style="border: darkorange 10px solid">
                    <%
                        fileServer.close();
                    } else {
                    %>
                    <img src="image/default_icon.png" class="img-circle icon_mouse" alt="default" width=170
                         height=170 data-toggle="modal" data-target="#changeIcon" style="border: darkorange 10px solid">
                    <%
                        }
                    %>
                    <div class="modal fade" id="changeIcon" role="dialog" aria-hidden="true"
                         aria-labelledby="changeIconLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-hidden="true"
                                            onclick="input_clear(document.getElementById('change_icon_form'))">
                                        &times;
                                    </button>
                                    <h4 class="modal-title" id="changeIconLabel">修 改 头 像</h4>
                                </div>
                                <div class="modal-body text-center">
                                    <br>
                                    <br>
                                    <form id="change_icon_form" method="post" enctype="multipart/form-data"
                                          action="change_icon" class="text-center">
                                        <input type="file" name="icon_file" accept="image/jpeg,image/png">
                                        <br>
                                        <br>
                                        <font color="red">请选择头像图片，只支持jpg（jpeg）和png图像</font>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-danger"
                                            onclick="form_clear(document.getElementById('change_icon_form'))">
                                        重 填
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('change_icon_form'))">
                                        修 改
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <br>
                    <br>
                    <hr class="split_line">
                    <br>
                    <br>
                    <table class="table table-striped text-center word_break">
                        <tr>
                            <td><b>用户名</b></td>
                            <td><b>真实姓名</b></td>
                            <td><b>性别</b></td>
                            <td><b>衣服尺码</b></td>
                            <td><b>电话</b></td>
                            <td><b>邮箱</b></td>
                            <td><b>用户类型</b></td>
                            <td><b>管理</b></td>
                        </tr>
                        <%
                            if (result.size() != 0) {
                        %>
                        <tr>
                            <td><%=result.getString("username")%>
                            </td>
                            <td><%=result.getString("real_name")%>
                            </td>
                            <td><%=result.getString("sex")%>
                            </td>
                            <td><%=result.getString("clothes_size")%>
                            </td>
                            <td><%=result.getString("phone")%>
                            </td>
                            <td><%=result.getString("email")%>
                            </td>
                            <td>

                                <%
                                    String type = result.getString("type");
                                    if (type.equals("0")) {
                                %>
                                <span class="btn btn-warning">参赛者</span>
                                <%
                                } else if (type.equals("1")) {
                                %>
                                <span class="btn btn-success">管理员</span>
                                <%
                                } else {
                                %>
                                <span class="btn btn-danger">未知</span>
                                <%
                                    }
                                    JSONObject teamJSON = settingServer.readSettingObject("team");
                                    boolean mutable = teamJSON.getBoolean("mutable");
                                %>
                            </td>
                            <td>
                                <button class="btn btn-info" data-toggle="modal" data-target="#changeMine">
                                    修改信息
                                </button>
                                <div class="modal fade" id="changeMine" role="dialog" aria-hidden="true"
                                     aria-labelledby="changeMineLabel">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal"
                                                        aria-hidden="true"
                                                        onclick="input_clear(document.getElementById('change_mine_form'))">
                                                    &times;
                                                </button>
                                                <h4 class="modal-title" id="changeMineLabel">修 改 信 息</h4>
                                            </div>
                                            <div class="modal-body">
                                                <form id="change_mine_form" action="change_information"
                                                      method="post">
                                                    <input type="text" name="oldUsername" hidden="hidden"
                                                           value="<%=user%>">
                                                    <br>
                                                    <br>
                                                    用&nbsp;户&nbsp;名:&nbsp;<input type="text"
                                                    <%
                                                            if(!mutable){
                                                            %>
                                                                                 readonly="readonly"
                                                    <%
                                                            }
                                                            %>
                                                                                 name="changeUser"
                                                                                 class="text-center"
                                                                                 value="<%=user%>">
                                                    <br>
                                                    <br>
                                                    真实姓名:&nbsp;&nbsp;<input type="text"
                                                                            name="realName"
                                                                            value="<%=result.getString("real_name")%>"
                                                                            class="text-center">
                                                    <br>
                                                    <br>
                                                    性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别:&nbsp;&nbsp;
                                                    <select class="text-center select_size" name="sex">
                                                        <%
                                                            if (result.getString("sex").equals("女")) {
                                                        %>
                                                        <option value="男">男</option>
                                                        <option value="女" selected="selected">女</option>
                                                        <%
                                                        } else {
                                                        %>
                                                        <option value="男" selected="selected">男</option>
                                                        <option value="女">女</option>
                                                        <%
                                                            }
                                                        %>
                                                    </select>
                                                    <br>
                                                    <br>
                                                    衣服尺码:&nbsp;&nbsp;
                                                    <select class="text-center select_size" name="clothes_size">
                                                        <%
                                                            String clothesSize = result.getString("clothes_size").toUpperCase();
                                                            if (clothesSize.equals("L")) {
                                                        %>
                                                        <option value="S">S</option>
                                                        <option value="M">M</option>
                                                        <option value="L" selected="selected">L</option>
                                                        <option value="XL">XL</option>
                                                        <option value="XXL">XXL</option>
                                                        <option value="XXXL">XXXL</option>
                                                        <%
                                                        } else if (clothesSize.equals("XL")) {
                                                        %>
                                                        <option value="S">S</option>
                                                        <option value="M">M</option>
                                                        <option value="L">L</option>
                                                        <option value="XL" selected="selected">XL</option>
                                                        <option value="XXL">XXL</option>
                                                        <option value="XXXL">XXXL</option>
                                                        <%
                                                        } else if (clothesSize.equals("S")) {
                                                        %>
                                                        <option value="S" selected="selected">S</option>
                                                        <option value="M">M</option>
                                                        <option value="L">L</option>
                                                        <option value="XL">XL</option>
                                                        <option value="XXL">XXL</option>
                                                        <option value="XXXL">XXXL</option>
                                                        <%
                                                        } else if (clothesSize.equals("XXL")) {
                                                        %>
                                                        <option value="S">S</option>
                                                        <option value="M">M</option>
                                                        <option value="L">L</option>
                                                        <option value="XL">XL</option>
                                                        <option value="XXL" selected="selected">XXL</option>
                                                        <option value="XXXL">XXXL</option>
                                                        <%
                                                        } else if (clothesSize.equals("XXXL")) {
                                                        %>
                                                        <option value="S">S</option>
                                                        <option value="M">M</option>
                                                        <option value="L">L</option>
                                                        <option value="XL">XL</option>
                                                        <option value="XXL">XXL</option>
                                                        <option value="XXXL" selected="selected">XXXL</option>
                                                        <%
                                                        } else {
                                                        %>
                                                        <option value="S" selected="selected">S</option>
                                                        <option value="M">M</option>
                                                        <option value="L">L</option>
                                                        <option value="XL">XL</option>
                                                        <option value="XXL">XXL</option>
                                                        <option value="XXXL">XXXL</option>
                                                        <%
                                                            }
                                                        %>
                                                    </select>
                                                    <br>
                                                    <br>
                                                    电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话:&nbsp;&nbsp;<input
                                                        type="text" name="phone"
                                                        value="<%=result.getString("phone")%>" class="text-center">
                                                    <br>
                                                    <br>
                                                    邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱:&nbsp;&nbsp;<input
                                                        type="text" name="email"
                                                        value="<%=result.getString("email")%>" class="text-center">
                                                    <br>
                                                    <br>
                                                </form>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-danger"
                                                        onclick="form_clear(document.getElementById('change_mine_form'))">
                                                    重 填
                                                </button>
                                                <button type="button" class="btn btn-primary"
                                                        onclick="form_submit(document.getElementById('change_mine_form'))">
                                                    修 改
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <button class="btn btn-info" data-toggle="modal"
                                        data-target="#changeMinePassword">修改密码
                                </button>
                                <div class="modal fade" id="changeMinePassword" role="dialog" aria-hidden="true"
                                     aria-labelledby="changeMinePasswordLabel">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal"
                                                        aria-hidden="true"
                                                        onclick="input_clear(document.getElementById('change_mine_password_form'))">
                                                    &times;
                                                </button>
                                                <h4 class="modal-title" id="changeMinePasswordLabel">修 改 密
                                                    码</h4>
                                            </div>
                                            <div class="modal-body">
                                                <form id="change_mine_password_form" action="change_password"
                                                      method="post">
                                                    <input type="text" name="changeUser" hidden="hidden"
                                                           value="<%=user%>">
                                                    <br>
                                                    <br>
                                                    旧&nbsp;&nbsp;密&nbsp;&nbsp;码:&nbsp;&nbsp;<input
                                                        type="password"
                                                        name="oldPassword" class="text-center">
                                                    <br>
                                                    <br>
                                                    密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;&nbsp;<input
                                                        type="password"
                                                        name="password" class="text-center">
                                                    <br>
                                                    <br>
                                                    再次输入:&nbsp;&nbsp;<input type="password" name="again"
                                                                            class="text-center">
                                                    <br>
                                                    <br>
                                                </form>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-danger"
                                                        onclick="form_clear(document.getElementById('change_mine_password_form'))">
                                                    重 填
                                                </button>
                                                <button type="button" class="btn btn-primary"
                                                        onclick="form_submit(document.getElementById('change_mine_password_form'))">
                                                    修 改
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <%
                        } else {
                        %>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                        <%
                            }
                        %>
                    </table>
                    <br>
                    <br>
                    <hr class="split_line">
                    <br>
                    <br>
                    <%
                        JSONArray resultArray = userServer.getUserRecordResultAnalysis(user.toString());
                        result = resultArray.getJSONObject(0);
                    %>
                    <div id="myResult" class="my_result"></div>
                    <script>
                        var myResult = echarts.init(document.getElementById('myResult'));
                        myResult.setOption({
                            title: {
                                text: '提交记录',
                                left: 'center',
                                right: 'center'
                            },
                            series: [
                                {
                                    name: '提交记录',
                                    type: 'pie',
                                    radius: ['25%', '50%'],
                                    data: [
                                        <%
                                        Iterator iterator = result.keys();
                                        String key;
                                        if(result.size()==0){
                                            %>
                                        {
                                            value: 0,
                                            name: '\n\n无提交记录'
                                        }
                                        <%
                                            }
                                        long count = 0;
                                        long sum = Long.parseLong(result.getString("sum"));
                                            while(iterator.hasNext()){
                                                key = iterator.next().toString();
                                                if(!key.equals("sum")){
                                                count = Long.parseLong(result.getString(key));
                                            %>
                                        {
                                            value:<%=count%>,
                                            name: '\n\n\n\n<%=key%>\n\n<%=count%> 次 / <%=sum%> 次\n\n占比 <%=((int)(((double)count/sum)*10000))/100.0%>%\n\n\n\n'
                                        },
                                        <%
                                        }
                                            }
                                        %>
                                    ]
                                }
                            ]
                        });
                    </script>
                    <br>
                    <br>
                    <table class="table table-striped text-center">
                        <tr>
                            <td><b>提交序号</b></td>
                            <td><b>题目</b></td>
                            <td><b>使用语言</b></td>
                            <td><b>结果</b></td>
                            <td><b>时间</b></td>
                            <td><b>内存</b></td>
                            <td><b>提交时间</b></td>
                            <td><b>查看详情</b></td>
                        </tr>
                        <%
                            LinkedList<LinkedHashMap<String, Object>> temp = userServer.getUserRecordResult(user.toString());
                            Iterator<LinkedHashMap<String, Object>> userRecordResultIterator = temp.iterator();
                            LinkedHashMap<String, Object> items;
                            SubjectServer subjectServer = new SubjectServer();
                            subjectServer.open();
                            count = temp.size();
                            while (userRecordResultIterator.hasNext()) {
                                items = userRecordResultIterator.next();
                        %>
                        <tr>
                            <td><%=count%>
                            </td>
                            <td>
                                <%=subjectServer.getSubjectInformationByID(items.get("subject_id").toString()).containsKey("title")?subjectServer.getSubjectInformationByID(items.get("subject_id").toString()).getString("title"):"特殊记录"%>
                            </td>
                            <td><%=items.get("language").toString().replace("CPP", "C++")%>
                            </td>
                            <td>
                                <%
                                    String submitResult = items.get("result").toString();
                                    if (submitResult.equals("Accepted")) {
                                %>
                                <span class="btn btn-success"><%=submitResult%></span>
                                <%
                                } else if (submitResult.equals("Compilation Error") || submitResult.equals("Time Limit Exceeded") || submitResult.equals("Memory Limit Exceeded") || submitResult.equals("Presentation Error")) {
                                %>
                                <span class="btn btn-warning"><%=submitResult%></span>
                                <%
                                } else if (submitResult.equals("Compile...") || submitResult.equals("Running...")) {
                                %>
                                <span class="btn btn-info"><%=submitResult%></span>
                                <%
                                } else if (submitResult.equals("RunTime Error")) {
                                %>
                                <span class="btn btn-primary"><%=submitResult%></span>
                                <%
                                } else {
                                %>
                                <span class="btn btn-danger"><%=submitResult%></span>
                                <%
                                    }
                                %>
                            </td>
                            <td><%=items.get("time")%> ms
                            </td>
                            <td><%=items.get("memory")%> kb
                            </td>
                            <td><%=items.get("submit_time")%>
                            </td>
                            <td>
                                <form id="code_<%=items.get("uuid")%>" method="post" action="code.jsp"
                                      target="_blank">
                                    <input type="text" name="recordID" value="<%=items.get("uuid")%>"
                                           hidden="hidden">
                                </form>
                                <button class="btn btn-info"
                                        onclick="form_submit(document.getElementById('code_<%=items.get("uuid")%>'))">
                                    查看代码
                                </button>
                            </td>
                        </tr>
                        <%
                                count--;
                            }
                            subjectServer.close();
                        %>
                    </table>
                    <br>
                    <br>
                    <%
                        userServer.close();
                    %>
                </div>
            </div>
        </div>
    </div>

        <%
    Decode decode = new Decode();
    JSONArray foot = settingServer.readSettingArray("foot");
    JSONObject foot0 = foot.getJSONObject(0);
    JSONObject foot1 = foot.getJSONObject(1);
    JSONObject foot2 = foot.getJSONObject(2);
%>
    <div class="foot">
        <row>
            <div class="col-xs-4 col-sm-4">
                <br>
                <div class="foot_item_title">
                    <p><%=decode.utf8Decode(foot0.getString("title"))%>
                    </p>
                </div>
                <hr class="foot_split">
                <div class="foot_item">
                    <p><%=decode.utf8Decode(foot0.getString("line0"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot0.getString("line1"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot0.getString("line2"))%>
                    </p>
                </div>
            </div>
            <div class="col-xs-4 col-sm-4">
                <br>
                <div class="foot_item_title">
                    <p><%=decode.utf8Decode(foot1.getString("title"))%>
                    </p>
                </div>
                <hr class="foot_split">
                <div class="foot_item">
                    <p><%=decode.utf8Decode(foot1.getString("line0"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot1.getString("line1"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot1.getString("line2"))%>
                    </p>
                </div>
            </div>
            <div class="col-xs-4 col-sm-4">
                <br>
                <div class="foot_item_title">
                    <p><%=decode.utf8Decode(foot2.getString("title"))%>
                    </p>
                </div>
                <hr class="foot_split">
                <div class="foot_item">
                    <p><%=decode.utf8Decode(foot2.getString("line0"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot2.getString("line1"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot2.getString("line2"))%>
                    </p>
                </div>
            </div>
        </row>
    </div>
        <%
    settingServer.close();
%>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/page.js"></script>
</html>
